/////////////////////////////////////////////////////////////////////////
// This is a servlet that will generate 30 users who are CURRENT students
/////////////////////////////////////////////////////////////////////////


package formServlets;
import java.io.IOException;

import javax.jdo.PersistenceManager;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.api.datastore.DatastoreAttributes;
import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.EntityNotFoundException;
import com.google.appengine.api.datastore.FetchOptions;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.PreparedQuery;
import com.google.appengine.api.datastore.Query;
import javax.servlet.http.HttpServlet;



public class  generateUsersServlet  extends HttpServlet  {



	// doGet forwards whatever requests to doPost, so get and Post do the same thing in this servlet.
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		getServletContext().getRequestDispatcher("SNSP.html").forward(req, resp);
	}
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		System.out.println("In generateUsersServlet");
		DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
		// 3 dummy schools, all in US.
		String [] schools = {"AAAschool", "BBBschool", "CCCschool"};
		String state = "MA";
		String country = "US";
		//
		String []majors = {"finance", "engineering", "philosophy"};
		
		for (int i = 0 ; i < 30; i++){
			// For entity UserSignUp
			
			String username = "user" + i;
			String password = "password";
			String email = username + "@fakeEmail.com";
			String seekingDegree = "MS";
			String previousGradSchool = "none";
			String graduateGPA = Double.toString( 3.0 + i * 0.1);
			String greScore = Double.toString(1500+i*10);
			String previousUndergrad = "Random School";
			String major = majors[i%3];
			String highSchool = "Random HS";
			String satScore = Double.toString(1200+i*10);
		
			Entity user = new Entity("UserSignUp",username);
			user.setProperty("Username", username);
			user.setProperty("Password", password);
			user.setProperty("email", email);
			user.setProperty("seekingDegree", seekingDegree);
			user.setProperty("previousGradSchool", previousGradSchool);
			user.setProperty("graduateGPA", graduateGPA);
			user.setProperty("greScore", greScore);
			user.setProperty("previousUndergrad",previousUndergrad);
			user.setProperty("major", major);
			user.setProperty("highSchool", highSchool);
			user.setProperty("satScore", satScore);
			
			datastore.put(user);
			
			// end UserSignUp
			
			// begin entity UserAcademicInfo
			
			String degreeType= seekingDegree;
			String schoolid = schools[i/10];
			
			////////////////////////////////////////////////////////////////////////////////////
			// Here we need to check if the school is present in the database
			// If it is a new school, we need to put in SchoolInfo and placeInfo for this school
			////////////////////////////////////////////////////////////////////////////////////
			
//			Query q1 = new Query("SchoolInfo");
//	        String countrylist=new String();
//			for (Entity result : q1.asIterable()) {
//				String schoolsInDatabaseID = (String) result.getProperty("schoolId");
//			}
			
			
			String startDate = "2009";
			String endDate = "no end";
			String entranceExamType = "GRE";
			String examScore = greScore;
			String funded = "Yes";
			String GPA= graduateGPA; 
			String Userid = username;
			user = new Entity("UserAcademicInfo",Userid);
			user.setProperty("Userid", Userid);
			user.setProperty("degreeType",degreeType);
			user.setProperty("schoolId", schoolid);
			user.setProperty("Major", major);
			user.setProperty("startDate", startDate);
			user.setProperty("endDate", endDate);
			user.setProperty("entranceExamType", entranceExamType);
			user.setProperty("examScore", examScore);
			user.setProperty("funded", funded);
			user.setProperty("GPA", GPA);
	        datastore.put(user);
	        
	        // end UserAcademicInfo
	        
	        // begin entity UserPersonalInfo
	        
			String firstname = "first" + i;
			String lastname = "last" + i;
			String gender = "M";
			String citizenship = "Chinese";
			Userid=username;
			user = new Entity("UserPersonalInfo",Userid);
			user.setProperty("Userid", Userid);
			user.setProperty("firstName", firstname);
			user.setProperty("lastName", lastname);
			user.setProperty("gender", gender);
			user.setProperty("citizenship", citizenship);
			datastore.put(user);
			
			// end UserPersonalInfo.
			
		} // end for loop
		
	}// end of Post
	
}// end of class
